package servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.PropertyConfigurator;

import database.PersistenceUtil;

@WebListener
public class AppContextListener implements ServletContextListener {

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		ServletContext context = sce.getServletContext();
		String log4jConfig = context.getInitParameter("log4j-properties-location");
		if(log4jConfig == null){
            System.err.println("Missing log4j-config init param");
            BasicConfigurator.configure();
		} else {
            PropertyConfigurator.configure(log4jConfig);
            System.out.println("log4j configured properly");
		}
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		PersistenceUtil.closeConnection();
		System.out.println("Connection closed");
	}

}
